package com.tcs.test.zk.base;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.CloseableUtils;
import org.junit.After;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BaseZookeeper {
	
	private static final Logger logger = LoggerFactory.getLogger(BaseZookeeper.class);
	
	private final String ZK_ADDRESS = "192.168.1.111:2181,192.168.1.113:2181,192.168.1.114:2181";
	
	protected CuratorFramework cf;

	@Before
	public void init() {
		// 重连策略,没1一秒重试一次,最大重试次数3次
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		cf = CuratorFrameworkFactory.builder().connectString(ZK_ADDRESS).sessionTimeoutMs(5000)
				.retryPolicy(retryPolicy).build();
		cf.start();
		logger.info("status : {} ", cf.getState());
	}
	
	@After
	public void close() {
		 CloseableUtils.closeQuietly(cf);
	}
}